extends CharacterBody2D

var value: int
var reflection_times: int = 0
const max_reflection_times: int = 5
func init(
	animation : SpriteFrames,
	pos : Vector2,
	shape : Shape2D,
	_value : int,
	trail_color: Color
) -> void:
	$AnimatedSprite2D.sprite_frames = animation
	$AnimatedSprite2D.play()
	$CollisionShape2D.shape = shape
	$Trail.default_color = trail_color

	position = pos
	value = _value
	
	

func _physics_process(delta) -> void:
	if Global.is_game_running:
		var collisionInfo : KinematicCollision2D = move_and_collide(velocity * delta)
		if velocity.x > - Global.G_scrollSpeed * 80:
				velocity.x -= 200 * delta 
		if $CollisionShape2D.disabled == false:
			if (position.x < 0) or (position.x > Global.W_resolution.x):
				queue_free()
				return

			if (position.y < 0) or (position.y > Global.W_resolution.y):
				velocity.y = - velocity.y
				
			
				
			if collisionInfo != null:
				collision_judgement(collisionInfo)

func collision_judgement(collisionInfo: KinematicCollision2D):
	var collider : CharacterBody2D = collisionInfo.get_collider()

	match collider.value + value:
		0, 2: # Reflection
			disable_collision_temporary(0.13)
			velocity.x = - velocity.x
			
			if Global.G_phase == Global.Phase.Night:
				if $"/root/Game/GlobalLightLevel".color.a > 0.04:
					$"/root/Game/GlobalLightLevel".color.a -= 0.04
				else:
					$"/root/Game/GlobalLightLevel".color.a = 0
			$"/root/Game/Bird/BeamCooldown".reduce(0.7)
			if reflection_times < max_reflection_times:
				reflection_times += 1
			else:
				queue_free()

		1: # Absorption
			$"/root/Game/UILayer/GamingUI/Beam".switch_texture()
			$"/root/Game/ParticleFactory".switch_beam_type()
			Global.G_score += 350
			$"/root/Game/Bird/BeamCooldown".reduce(1.8)
			$"/root/Game/Sound/ScoredSound".play()
			$"/root/Game/UILayer/GamingUI/Scored".play("Scored")
			collider.queue_free()
			queue_free()

		6: # Refraction
			var beam_1 = $"/root/Game/ParticleFactory".spawn_beam(position)
			var beam_2 = $"/root/Game/ParticleFactory".spawn_beam(position)
			
			beam_1.velocity = velocity.rotated(PI/4)
			beam_2.velocity = velocity.rotated(-PI/4)
			beam_1.get_node("Trail").default_color = $"/root/Game/Mechanics".randomize_trail_color()
			beam_2.get_node("Trail").default_color = $"/root/Game/Mechanics".randomize_trail_color()

			beam_1.disable_collision_temporary(0.11)
			beam_2.disable_collision_temporary(0.11)

			collider.queue_free()
			queue_free()

		-1: # Interference
			$"/root/Game/ObstacleFactory".next_spawn_interfered = true
			collider.get_node("Sprite2D").texture = Preload.SeamCharged_Obstacle
			$"/root/Game/Bird/BeamCooldown".reduce(4.5)
			$"/root/Game/UILayer/GamingUI/DiffractionHint".show()
			$"/root/Game/Mechanics/HintDisappearTimer2".start()
			queue_free()

		11: # Diffraction 
			$DiffractionTimer.start()
			Global.G_scrollSpeed = Global.G_baseScrollSpeed * 3
			velocity *= 0.4
			$"/root/Game/Bird".hide()
			$"/root/Game/Bird/CollisionShape2D".disabled = true
			value = -5
			Global.G_natural_score_increase = 32
			disable_collision_temporary(3)
			$DeletionTimer.stop()
			$"/root/Game/Mechanics/ObstacleSpawnTimer".wait_time = 0.5
		-2:  # Ending 2
			
			$"/root/Game/Achievement".achieve("Ending2")
			queue_free()

		_:
			disable_collision_temporary(1)

func _on_diffraction_timer_timeout():
	value = 0
	velocity *= 2.5
	
	if $"/root/Game/Achievement".is_achieved("Ending1"):
		if Global.G_phase == Global.Phase.Day:
			Global.G_scrollSpeed = Global.G_baseScrollSpeed * 1.3
			Global.G_natural_score_increase = 10
			$"/root/Game/Mechanics/ObstacleSpawnTimer".wait_time = 1.2
		else:
			Global.G_scrollSpeed = Global.G_baseScrollSpeed * 1.8
			Global.G_natural_score_increase = 25
			$"/root/Game/Mechanics/ObstacleSpawnTimer".wait_time = 1
	
	else:	
		Global.G_scrollSpeed = Global.G_baseScrollSpeed * 1.5
		Global.G_natural_score_increase = 20
		$"/root/Game/Mechanics/ObstacleSpawnTimer".wait_time = 1.3
		
	
	$"/root/Game/Bird/BeamCooldown".stop()
	$"/root/Game/Bird".BeamReady = true
	$"/root/Game/ParticleFactory".CurrentBeamType = $"/root/Game/ParticleFactory".BeamType.Dark
	$"/root/Game/UILayer/GamingUI/Beam".texture = Preload.DarkBeamSkill
	
	$"/root/Game/Bird".position = Vector2(200, 250)
	$"/root/Game/Bird".disable_collision_temporary(3)
	$"/root/Game/Bird".show()
	queue_free()
	if $"/root/Game/GlobalLightLevel".color.a > 0.3:
		$"/root/Game/GlobalLightLevel".color.a -= 0.3
	else:
		$"/root/Game/GlobalLightLevel".color.a = 0

func disable_collision_temporary(time: float):
	$CollisionShape2D.disabled = true
	
	$CollisionDisabledTimer.wait_time = time
	$CollisionDisabledTimer.start()

func enable_collision(): 
	$CollisionShape2D.disabled = false
	
func _on_deletion_timer_timeout():
	queue_free()
